<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script>
    function Queue(){
        this.data=[];
    }
    Queue.prototype={
        constructor:Queue,
        enqueue: function (elm) {//入队
            this.data.push(elm);
        },
        dequeue: function () {//出队
            return this.data.shift();
        },
        front: function () {//获取队首元素
            return this.data[0];
        },
        back: function () {//获取队尾元素
            return this.data[this.length-1];
        },
        clear: function () {//清空
            delete this.data;
            this.data=[];
        },
        isEmpty: function () {//是否为空
            return this.data.length==0?true:false;
        },
        toString: function () {//显示
            return this.data.join();
        }
    }

    var distribute=function(){
        var digits=[1,10];
        return function (nums,digit) {
            var i, j, k, l,num;
            if(void 0==digit){
                distribute(nums,digits.shift());
            }else{
                var queues=[];
                for(i=0;i<10;i++){
                    queues[i]=new Queue();
                }
                for(i=0,l=nums.length;i<l;i++){
                    num=nums[i];
                    if(1===digit){
                        queues[num%10].enqueue(num);
                    }else{
                        queues[Math.floor(num/10)].enqueue(num);
                    }
                }
                nums.length=0;
                for(j=0,k=0;j<10;j++){
                    while(!queues[j].isEmpty()){
                        nums[k]=queues[j].dequeue();
                        k++;
                    }
                }
                if(digits.length){
                    distribute(nums);
                }else{
                    return nums;
                }
            }
        }
    }();

    var nums=[92,46,99,15,93,35,52,1];
    nums=function(){
        var list=[],l=10;
        while(l--){
            list.push(Math.random()*100|0);
        }
        return list;
    }();
    console.log('nums:',nums);
    distribute(nums);
    console.log('排序后:',nums);
</script>
</body>
</html>